home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / microemacs / fileio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  122 lines

  1.  
  2. /*
  3.  * The routines in this file read and write ASCII files from the disk. All of
  4.  * the knowledge about files are here. A better message writing scheme should
  5.  * be used.
  6.  */
  7. #include        <stdio.h>
  8. #include        "ed.h"
  9.  
  10. FILE    *ffp;                           /* File pointer, all functions. */
  11.  
  12. /*
  13.  * Open a file for reading.
  14.  */
  15. ffropen(fn)
  16. char    *fn;
  17. {
  18.         if ((ffp=fopen(fn, "r")) == NULL)
  19.                 return (FIOFNF);
  20.         return (FIOSUC);
  21. }
  22.  
  23. /*
  24.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  25.  * (cannot create).
  26.  */
  27. ffwopen(fn)
  28. char    *fn;
  29. {
  30. #if     VMS
  31.         register int    fd;
  32.  
  33.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  34.         || (ffp=fdopen(fd, "w")) == NULL) {
  35. #else
  36.         if ((ffp=fopen(fn, "w")) == NULL) {
  37. #endif
  38.                 mlwrite("Cannot open file for writing");
  39.                 return (FIOERR);
  40.         }
  41.         return (FIOSUC);
  42. }
  43.  
  44. /*
  45.  * Close a file. Should look at the status in all systems.
  46.  */
  47. ffclose()
  48. {
  49. #if     V7
  50.         if (fclose(ffp) != FALSE) {
  51.                 mlwrite("Error closing file");
  52.                 return(FIOERR);
  53.         }
  54.         return(FIOSUC);
  55. #endif
  56.         fclose(ffp);
  57.         return (FIOSUC);
  58. }
  59.  
  60. /*
  61.  * Write a line to the already opened file. The "buf" points to the buffer,
  62.  * and the "nbuf" is its length, less the free newline. Return the status.
  63.  * Check only at the newline.
  64.  */
  65. ffputline(buf, nbuf)
  66. char    buf[];
  67. {
  68.         register int    i;
  69.  
  70.         for (i = 0; i < nbuf; ++i)
  71.                 fputc(buf[i]&0xFF, ffp);
  72.  
  73.         fputc('\n', ffp);
  74.  
  75.         if (ferror(ffp)) {
  76.                 mlwrite("Write I/O error");
  77.                 return (FIOERR);
  78.         }
  79.  
  80.         return (FIOSUC);
  81. }
  82.  
  83. /*
  84.  * Read a line from a file, and store the bytes in the supplied buffer. The
  85.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  86.  * at the end of the file that don't have a newline present. Check for I/O
  87.  * errors too. Return status.
  88.  */
  89. ffgetline(buf, nbuf)
  90. register char   buf[];
  91. {
  92.         register int    c;
  93.         register int    i;
  94.  
  95.         i = 0;
  96.  
  97.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  98.                 if (i >= nbuf-1) {
  99.                         mlwrite("File has long line");
  100.                         return (FIOERR);
  101.                 }
  102.                 buf[i++] = c;
  103.         }
  104.  
  105.         if (c == EOF) {
  106.                 if (ferror(ffp)) {
  107.                         mlwrite("File read error");
  108.                         return (FIOERR);
  109.                 }
  110.  
  111.                 if (i != 0) {
  112.                         mlwrite("File has funny line at EOF");
  113.                         return (FIOERR);
  114.                 }
  115.                 return (FIOEOF);
  116.         }
  117.  
  118.         buf[i] = 0;
  119.         return (FIOSUC);
  120. }
  121.  
  122.